<?php
namespace App\Presenters;

use App\Repositories\PermissionRepository;
use Auth;

class MenuPresenter
{
    protected $permission;

    /**
     * MenuPresenter constructor.
     * @param PermissionRepository $repository
     */
    public function __construct(PermissionRepository $repository)
    {
        $this->permission = $repository;
    }
    /**
     * 创建编辑权限时能选择的权限列表
     * @param int $pid
     * @return string
     */
    public function permissionSelect($pid = 0)
    {
        $menus = buildPermission($this->permission->getMenu());
        $select = '<select class="form-control" name="pid" id="pid">';
        $select .= '<option value="0">--顶级权限--</option>';
        if($menus) {
            foreach ($menus as $menu) {
                if($menu->id == $pid) {
                    $select .= '<option  value="' . $menu['id'] . '" selected >' . $menu['display_name'] . '[' . $menu['name'] . ']</option>';
                } elseif ($menu->id != $pid) {
                    $select .= '<option  value="' . $menu['id'] . '">' . $menu['display_name'] . '[' . $menu['name'] . ']</option>';
                    foreach ($menu['child'] as $child) {
                        $select .= '<option  value="' . $child['id'] . '">┗━ ' . $child['display_name'] . '[' . $child['name'] . ']</option>';
                        if ($child->id == $pid){
                            $select .= '<option  value="' . $child['id'] . '" selected >┗━ '. $child['display_name'] . '[' . $child['name'] . ']</option>';
                        }
                    }
                }
            }
        }
        $select .= '</select>';
        return $select;
    }
    /**
     * 根据用户权限显示菜单
     * @return string
     */
    public function menu()
    {
        //获取所有菜单
        $menus = buildPermission($this->permission->getMenu());
        $html = '';
        if ($menus) {
            foreach ($menus as $menu) {
                //判断当前用户是否拥有该权限
                if (Auth::guard()->user()->can($menu['name'])){
                    //有权限生成一级菜单
                    $html .= '<li class="nav-item">';
                    //判断菜单是否包含 '.' ，如果包含则为二级菜单项，否则为一级菜单项，一级菜单项可点击没有页面
                    //$href = (str_contains($menu['name'], '.')) ? route($menu['name']) : '#';
                    $html .= '<a href="javascript:;" class="nav-link nav-toggle"><i class="fa ' . $menu['icon'] . '"></i><span class="title"> ' . $menu['display_name'] . '</span><span class="arrow"></span></a>';
                    $html .= '<ul class="sub-menu">';
                    foreach ($menu['child'] as $child) {
                        if (Auth::guard()->user()->can($child['name'])) {
                            //有权限生成二级菜单
                            $html .= '<li class="nav-item">';
                            //$href = (str_contains($child['name'], '.')) ? route($child['name']) : '#';
                            $html .= '<a href="' . route($child['name']) . '" class="nav-link "><i class="fa ' . $child['icon'] . '"></i><span class="title"> ' . $child['display_name'] . '</span></a>';
                            $html .= '</li>';
                        }
                    }
                    $html .= '</ul>';
                    $html .= '</li>';
                }
            }
        }
        return $html;
    }

}